﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CreateLookupColumn.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Creates or Updates a Content Type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Actions.Wss.Column
{
    using System.Collections.Generic;

    using Entities;

    using Engine;

    using SharePoint.Wss.Attributes;

    using Utils;
    using SharePoint.Wss.Lists;
    using Microsoft.SharePoint;

    /// <summary>
    /// Creates a lookup column from a list.
    /// </summary>
    [CollaborisClass(
        Title = "Creates a new Lookup Column in the Current Web.", 
        ReleaseStatus = ReleaseStatus.Stable, 
        DisableDocumentation = false, 
        ContactAuthorEmail = "Support@Collaboris.co.uk",
        Summary = "Creates a new <b>site</b>-scoped lookup column pointing to a field in an existing list.",
          ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C631}")]
    public class CreateLookupColumn : WSSActionBase<CreateLookupColumnInfo>
    {
        #region Helper Methods

        /// <summary>
        /// Executes the Action using the values passed in by the Data Entity
        /// and the <see cref="SafContext"/>.
        /// </summary>
        /// <param name="context">
        /// A valid <see cref="SafContext"/> containing properties to run the Action with.
        /// </param>
        /// <param name="entity">
        /// A Valid <see cref="AddSiteColumnToListInfo"/> containing the parameters with which to run the Action with.
        /// </param>
        /// <returns>
        /// A <see cref="ProcessStatus"/> signifying the outcome of processing the Action.
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, CreateLookupColumnInfo entity)
        {
            this.Trace.TraceMethodStart("DoAction");
            ValidationHelper.VerifyObjectArgument(context.Site, "context.Site");

            SPFieldLookup lookupField = SPListHelper.CreateLookupField(entity.FieldName,
                                                                       entity.Group,
                                                                       entity.Required,
                                                                       entity.AllowMultipleValues,
                                                                       TargetWeb,
                                                                       entity.LookupListUrl,
                                                                       entity.LookupFieldName);

            if (lookupField == null)
            {
                this.Trace.TraceWarning("Failed to create the lookup.");
                return ProcessStatus.Error;
            }

            this.Trace.TraceMethodEnd("DoAction");
            return ProcessStatus.Success;
        }

        /// <summary>
        /// Checks the Data entity used by the Action for Validity
        /// </summary>
        /// <param name="context">
        /// The <see cref="SafContext"/> passed in by the SAF Engine.
        /// </param>
        /// <param name="entity">
        /// The data entity to Validate.
        /// </param>
        /// <param name="errors">
        /// List of <see cref="ValidationError"/>s
        /// </param>
        /// <param name="mode">
        /// <see cref="ProcessMode"/>signalling wheter we are Doing, or Undoing the action.
        /// </param>
        /// <returns>
        /// false if invalid; otherwise true.
        /// </returns>
        protected override bool IsValid(
            SafContext context, CreateLookupColumnInfo entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;

            // Validate each object now.
            ValidateStringForNull(entity.FieldName, "CreateLookupColumnInfo.FieldName", ref errors);
            ValidateStringForNull(entity.Group, "CreateLookupColumnInfo.Group", ref errors);
            ValidateObjectForNull(entity.AllowMultipleValues, "CreateLookupColumnInfo.AllowMultipleValues", ref errors);
            ValidateObjectForNull(entity.Required, "CreateLookupColumnInfo.Required", ref errors);
            ValidateStringForNull(entity.LookupListUrl, "CreateLookupColumnInfo.LookupListUrl", ref errors);
            ValidateStringForNull(entity.LookupFieldName, "CreateLookupColumnInfo.LookupFieldName", ref errors);

            // Have we added errors ?
            return errors.Count == currentCount;
        }

        #endregion
    }
}